<template>
    <div>
        <el-tabs type="border-card" class="main" @tab-click="getList">
            <el-tab-pane label="任务信息">
                <el-row>
                    <el-col :span="24"> <strong>任务标题:</strong> {{ taskInfos.name }} </el-col>
                </el-row>
                <br />
                <el-row>
                    <el-col :span="8"><strong>年度: </strong> {{ targetYear }} </el-col>
                    <el-col :span="8">
                        <strong>任务属性:</strong>
                        <span v-if="taskInfos.type == 0">A类</span>
                        <span v-if="taskInfos.type == 1">B类</span>
                        <span v-if="taskInfos.type == 2">C类</span>
                    </el-col>
                    <el-col :span="8">
                        <strong>分配次级:</strong>
                        <span v-if="taskInfos.level == 0">一级</span>
                        <span v-if="taskInfos.level == 1">二级</span>
                        <span v-if="taskInfos.level == 2">三级</span>
                    </el-col>
                </el-row>
                <br />
                <el-row>
                    <el-col :span="8"> <strong>任务下发人:</strong> {{ taskInfos.allocUserId }} </el-col>
                    <el-col :span="8"> <strong>任务下发部门: </strong>{{ taskInfos.allocDeptId }} </el-col>
                    <el-col :span="8"> <strong>任务接受部门: </strong>{{ taskInfos.acceptDeptId }} </el-col>
                </el-row>
                <br />
                <el-row>
                    <el-col :span="24">
                        <strong>是否为学校专项考核:</strong>
                        <span v-if="taskInfos.isAssess == null">--</span>
                        <span v-if="taskInfos.isAssess == 1">是</span>
                        <span v-if="taskInfos.isAssess == 0">否</span>
                    </el-col>
                </el-row>
                <br />
                <el-row>
                    <el-col :span="24"> <strong>任务要求:</strong> {{ taskInfos.requirement }} </el-col>
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="下发任务" v-if="isDept">
                <div>
                    <el-button type="warning" @click="addDialog" v-show="isPermissionValue">新增任务</el-button>
                </div>
                <br />
                <el-table :data="taskData" style="width: 100%" border>
                    <el-table-column prop="number" label="序号" width="50"> </el-table-column>
                    <el-table-column prop="name" label="任务标题"> </el-table-column>
                    <el-table-column prop="acceptDeptId" label="接受部门" width="150"> </el-table-column>
                    <el-table-column prop="type" label="任务属性" width="100">
                        <template slot-scope="scope">
                            <div v-if="scope.row.type == 0">A</div>
                            <div v-else-if="scope.row.type == 1">B</div>
                            <div v-else-if="scope.row.type == 2">C</div>
                            <div v-else></div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="level" label="分配次级" width="100">
                        <template slot-scope="scope">
                            <div v-if="scope.row.level == 0">一级</div>
                            <div v-else-if="scope.row.level == 1">二级</div>
                            <div v-else-if="scope.row.level == 2">三级</div>
                            <div v-else></div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isAssess" label="学校年度专项考核" width="130">
                        <template slot-scope="scope">
                            <div v-if="scope.row.isAssess == 0">否</div>
                            <div v-else-if="scope.row.isAssess == 1">是</div>
                            <div v-else></div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150" v-if="isPermissionValue">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="editTaskById(scope.row.id)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="deleteTaskById(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="布置工作">
                <div>
                    <el-button type="warning" @click="addWorlDialog" v-show="isPermissionValue">新增工作</el-button>
                </div>
                <br />
                <el-table :data="workData" style="width: 100%" border>
                    <el-table-column prop="number" label="序号"> </el-table-column>
                    <el-table-column prop="name" label="任务标题"> </el-table-column>
                    <el-table-column prop="managerId" label="负责人"> </el-table-column>
                    <el-table-column prop="helperIds" label="协助人"> </el-table-column>
                    <el-table-column prop="startTime" label="开始日期"> </el-table-column>
                    <el-table-column prop="endTime" label="完成日期"> </el-table-column>
                    <el-table-column prop="isAssess" label="属于学校专项考核工作">
                        <template slot-scope="scope">
                            <div v-if="scope.row.isAssess == 0">否</div>
                            <div v-else-if="scope.row.isAssess == 1">是</div>
                            <div v-else></div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150" v-if="isPermissionValue">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="editWorkById(scope.row.id)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="deleteWorkById(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
        </el-tabs>
        <!--新增任务对话框-->
        <el-dialog title="下发任务" :visible.sync="addTaskDialogVisible" width="90%" style="min-width: 1450px">
            <el-form :model="addTaskInfoForm" label-width="100px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item>
                            <span style="over-flow: hidden">序号</span>
                            <el-input v-model="addTaskInfoForm.number"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item>
                            <span>任务标题</span>
                            <el-input v-model="addTaskInfoForm.name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item>
                            <span>任务属性</span>
                            <el-input v-model="addTaskInfoForm.type" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item>
                            <span>任务分配次级</span>
                            <el-input v-model="addTaskInfoForm.level" :disabled="true"> </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="">
                            <span>任务下发人</span>
                            <!-- <el-select v-model="addTaskInfoForm.allocUserId" filterable>
                                <el-option v-for="item in teacherOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select> -->
                            <div>
                                <strong>{{ addTaskInfoForm.allocUserId }}</strong>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="">
                            <span>任务下发部门</span>
                            <el-select v-model="addTaskInfoForm.allocDeptId" filterable disabled>
                                <el-option v-for="item in departmentOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                            <!-- <div>
                                <strong>{{ addTaskInfoForm.allocDeptId }}</strong>
                            </div> -->
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="">
                            <span>任务接受部门</span>
                            <el-select v-model="addTaskInfoForm.acceptDeptId" filterable>
                                <el-option v-for="item in departmentOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item>
                            <span>是否列为学校年度专项考核</span>
                            <br />
                            <el-select v-model="addTaskInfoForm.isAssess" placeholder="--">
                                <el-option v-for="item in collegeYearTestoptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item label="">
                    <span>任务要求</span>
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="addTaskInfoForm.requirement"> </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="success" @click="addTaskByIdType">保存</el-button>
                <el-button @click="cancelAddDialog">取消</el-button>
            </span>
        </el-dialog>

        <!--修改任务对话框-->
        <el-dialog title="修改任务" :visible.sync="eidtTaskDialogVisible" width="90%" style="min-width: 1450px">
            <el-form :model="editTaskInfoForm" label-width="100px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item>
                            <span style="over-flow: hidden">序号</span>
                            <el-input v-model="editTaskInfoForm.number"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item>
                            <span>任务标题</span>
                            <el-input v-model="editTaskInfoForm.name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item>
                            <span>任务属性</span>
                            <el-input v-model="editTaskInfoForm.type" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item>
                            <span>任务分配次级</span>
                            <el-input v-model="editTaskInfoForm.level" :disabled="true"> </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="">
                            <span>任务下发人</span>
                            <el-select v-model="editTaskInfoForm.allocUserId" filterable disabled>
                                <el-option v-for="item in teacherOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="">
                            <span>任务下发部门</span>
                            <el-select v-model="editTaskInfoForm.allocDeptId" filterable disabled>
                                <el-option v-for="item in departmentOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="">
                            <span>任务接受部门</span>
                            <el-select v-model="editTaskInfoForm.acceptDeptId" filterable>
                                <el-option v-for="item in departmentOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item>
                            <span>是否列为学校年度专项考核</span>
                            <br />
                            <el-select v-model="editTaskInfoForm.isAssess" placeholder="--">
                                <el-option v-for="item in collegeYearTestoptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item label="">
                    <span>任务要求</span>
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="editTaskInfoForm.requirement"> </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="success" @click="pushEditTask">保存</el-button>
                <el-button @click="cancelEditTask">取消</el-button>
            </span>
        </el-dialog>

        <!--新增工作对话框-->
        <el-dialog title="新增工作" :visible.sync="addWorkDialogVisible" width="90%" style="min-width: 1423px">
            <el-form :model="addWorkInfoForm" label-width="100px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item>
                            <span style="over-flow: hidden">序号</span>
                            <el-input v-model="addWorkInfoForm.number"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item>
                            <span>任务标题</span>
                            <el-input v-model="addWorkInfoForm.name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="2">
                        <el-form-item>
                            <span>是否列为学校年度专项考核</span>
                            <br />
                            <el-select v-model="addWorkInfoForm.isAssess" placeholder="--">
                                <el-option v-for="item in collegeYearTestoptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>负责人</span>
                            <el-select v-model="addWorkInfoForm.managerId" filterable>
                                <el-option v-for="item in teacherOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>协助人</span>
                            <el-select v-model="addWorkInfoForm.helperIds" multiple filterable>
                                <el-option v-for="item in teacherOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>开始日期</span>
                            <el-date-picker v-model="addWorkInfoForm.startTime" type="date" placeholder="选择日期"> </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>结束日期</span>
                            <el-date-picker v-model="addWorkInfoForm.endTime" type="date" placeholder="选择日期"> </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="">
                    <span>任务要求</span>
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 7 }" v-model="addWorkInfoForm.requirement"> </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="success" @click="addWorkByTargetId">保存</el-button>
                <el-button @click="cancelAddWorkByTargetId">取消</el-button>
            </span>
        </el-dialog>

        <!--修改工作对话框-->
        <el-dialog title="修改工作" :visible.sync="editWorkDialogVisible" width="90%" style="min-width: 1423px">
            <el-form :model="editWorkInfoForm" label-width="100px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item>
                            <span style="over-flow: hidden">序号</span>
                            <el-input v-model="editWorkInfoForm.number"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item>
                            <span>任务标题</span>
                            <el-input v-model="editWorkInfoForm.name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="2">
                        <el-form-item>
                            <span>是否列为学校年度专项考核</span>
                            <br />
                            <el-select v-model="editWorkInfoForm.isAssess" placeholder="--">
                                <el-option v-for="item in collegeYearTestoptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>负责人</span>
                            <el-select v-model="editWorkInfoForm.managerId" filterable>
                                <el-option v-for="item in teacherOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>协助人</span>
                            <el-select v-model="editWorkInfoForm.helperIds" multiple filterable>
                                <el-option v-for="item in teacherOptions" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>开始日期</span>
                            <el-date-picker v-model="editWorkInfoForm.startTime" type="date" placeholder="选择日期"> </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="">
                            <span>结束日期</span>
                            <el-date-picker v-model="editWorkInfoForm.endTime" type="date" placeholder="选择日期"> </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="">
                    <span>任务要求</span>
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 7 }" v-model="editWorkInfoForm.requirement"> </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="success" @click="pushEditWorkById">保存</el-button>
                <el-button @click="cancelEditWork">取消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import moment from 'moment';
import workOfYearTargetApi from '../../../../api/workOfYearTargetApi';
import workApi from '../../../../api/work';
import userApi from '../../../../api/user';
import { mapState } from 'vuex';
import LoginVue from '../../Login.vue';

export default {
    props: {
        taskInfos: Object,
        // 年份名字
        targetYear: String,
        // 年份id
        targetId: String
    },
    data() {
        return {
            editWorkDialogVisible: false,
            editWorkInfoForm: {},
            // 布置工作列表
            workData: [],
            // 下发任务列表
            taskData: [],
            // 新增工作开关
            addWorkDialogVisible: false,
            addTaskDialogVisible: false,
            eidtTaskDialogVisible: false,

            // 新增编辑工作表单
            addWorkInfoForm: {},
            editWorkInfoForm: {},
            // 新增编辑任务表单
            addTaskInfoForm: {},
            editTaskInfoForm: {},
            teacherOptions: [],
            departmentOptions: [],
            departmentId: '',
            collegeYearTestoptions: [
                {
                    value: '',
                    label: '--'
                },
                {
                    value: 1,
                    label: '是'
                },
                {
                    value: 0,
                    label: '否'
                }
            ],
            tempTeachers: [],
            tempDeparts: [],
            allocDeptId: '',
            editTemp: '',
            // 判断不是为二级院部
            isDept: true,
            isPermissionValue: true
        };
    },
    methods: {
        isPermission() {
            let dempartmentLabel = this.$store.state.user.deptId;
            for (let z = 0; z < this.$store.state.department.department.length; ++z) {
                if (this.$store.state.department.department[z].id == dempartmentLabel) {
                    dempartmentLabel = this.$store.state.department.department[z].name;
                    break;
                }
            }
            if (dempartmentLabel == this.taskInfos.acceptDeptId) {
                this.isPermissionValue = true;
            } else {
                this.isPermissionValue = false;
            }
        },
        // 根据年度目标任务的id和等级添加任务
        async addTaskByIdType() {
                        if (isNaN(this.addTaskInfoForm.number)) {
                this.$alert('序号非法！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (
                this.addTaskInfoForm.acceptDeptId == '' ||
                this.addTaskInfoForm.acceptDeptId == null ||
                this.addTaskInfoForm.acceptDeptId == undefined
            ) {
                this.$alert('任务接收部门不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.addTaskInfoForm.number == '' || this.addTaskInfoForm.number == null || this.addTaskInfoForm.number == undefined) {
                this.$alert('序号不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.addTaskInfoForm.name == '' || this.addTaskInfoForm.name == null || this.addTaskInfoForm.name == undefined) {
                this.$alert('任务标题不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            this.addTaskInfoForm.type = 1;
            this.addTaskInfoForm.level = 2;
            this.addTaskInfoForm.yearTargetId = this.targetId;
            this.addTaskInfoForm.parentId = this.taskInfos.id;
            this.addTaskInfoForm.allocUserId = this.$store.state.user.userId;
            for (let i = 0; i < this.$store.state.department.department.length; ++i) {
                if (this.addTaskInfoForm.allocDeptId == this.$store.state.department.department[i].name) {
                    this.addTaskInfoForm.allocDeptId = this.$store.state.department.department[i].id;
                }
            }
            // console.log(this.addTaskInfoForm);
            await workOfYearTargetApi.addWorkOfYearTarget(this.targetId, this.addTaskInfoForm.type, this.addTaskInfoForm).then((res) => {
                this.$message.success('新增任务成功');
                this.addTaskInfoForm = {};
            });
            this.getList();
            this.addTaskDialogVisible = false;
        },
        // 新增工作方法
        addWorlDialog() {
            this.addWorkDialogVisible = true;
            this.addWorkInfoForm = {};
        },
        //  编辑任务
        async editTaskById(id) {
            await workOfYearTargetApi.getWorkOfYearTargetById(id).then((res) => {
                this.editTaskInfoForm = res.data;
                this.editTaskInfoForm.type = 'B类';
                this.editTaskInfoForm.level = '三级';
                this.editTemp = this.editTaskInfoForm.allocDeptId;
                for (let j = 0; j < this.$store.state.department.department.length; ++j) {
                    if (this.$store.state.department.department[j].type == 2) {
                        let temp = {
                            value: this.$store.state.department.department[j].id,
                            label: this.$store.state.department.department[j].name
                        };
                        this.departmentOptions.push(temp);
                    }
                }
            });
            this.eidtTaskDialogVisible = true;
        },
        async pushEditTask() {
                        if (isNaN(this.editTaskInfoForm.number)) {
                this.$alert('序号非法！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
             if (
                this.editTaskInfoForm.acceptDeptId == '' ||
                this.editTaskInfoForm.acceptDeptId == null ||
                this.editTaskInfoForm.acceptDeptId == undefined
            ) {
                this.$alert('任务接收部门不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.editTaskInfoForm.number == '' || this.editTaskInfoForm.number == null || this.editTaskInfoForm.number == undefined) {
                this.$alert('序号不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.editTaskInfoForm.name == '' || this.editTaskInfoForm.name == null || this.editTaskInfoForm.name == undefined) {
                this.$alert('任务标题不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            this.editTaskInfoForm.type = 1;
            this.editTaskInfoForm.level = 2;
            await workOfYearTargetApi.editWorkOfYearTarget(this.editTaskInfoForm.id, this.editTaskInfoForm).then((res) => {
                this.$message.success('更改任务信息成功！');
                this.eidtTaskDialogVisible = false;
                this.getTaskTwoLevelByAllocateId();
                this.$forceUpdate();
            });
        },
        cancelEditTask() {
            this.eidtTaskDialogVisible = false;
            this.editTaskInfoForm = {};
        },
        async deleteTaskById(id) {
            const confirmResult = await this.$confirm('此操作将永久删除该项目, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch((err) => err);
            if (confirmResult != 'confirm') {
                return;
            }
            if (confirmResult == 'confirm') {
                await workOfYearTargetApi.removeWorkOfYearTarget(id).then((res) => {
                    this.$message({
                        message: '删除目标任务成功！',
                        type: 'success'
                    });
                });
                this.getTaskTwoLevelByAllocateId();
                this.getList();
            }
        },

        // 打开add对话框
        addDialog() {
            this.addTaskInfoForm.type = 'B类';
            this.addTaskInfoForm.level = '三级';
            this.addTaskInfoForm.allocUserId = this.$store.state.user.name;
            this.addTaskInfoForm.allocDeptId = this.taskInfos.acceptDeptId;
            for (let j = 0; j < this.$store.state.department.department.length; ++j) {
                if (this.$store.state.department.department[j].type == 2) {
                    let temp = {
                        value: this.$store.state.department.department[j].id,
                        label: this.$store.state.department.department[j].name
                    };
                    this.departmentOptions.push(temp);
                }
            }
            this.addTaskDialogVisible = true;
        },
        cancelAddDialog() {
            this.addTaskInfoForm = {};
            this.addTaskDialogVisible = false;
        },
        //获取老师姓名
        async getTeachersAndDepartments() {
            this.$store.dispatch('getTeachers').then((res) => {
                this.tempTeachers = this.$store.state.teacher.teachers;
                for (let i = 0; i < this.tempTeachers.length; ++i) {
                    this.teacherOptions[i] = {
                        value: this.tempTeachers[i].id,
                        label: this.tempTeachers[i].name
                    };
                }
            });

            this.$store.dispatch('getDepartment').then((res) => {
                this.tempDeparts = this.$store.state.department.department;
                // for (let j = 0; j < this.tempDeparts.length; ++j) {
                //     this.departmentOptions[j] = {
                //         value: this.tempDeparts[j].id,
                //         label: this.tempDeparts[j].name
                //     };
                // }
                for (let n = 0; n < this.tempDeparts.length; ++n) {
                    if (this.tempDeparts[n].name == this.taskInfos.acceptDeptId) {
                        this.departmentId = this.tempDeparts[n].id;
                    }
                }
            });
            let tempAcceptDeptid = '';
            for (let m = 0; m < this.$store.state.department.department.length; ++m) {
                if (this.taskInfos.acceptDeptId == this.$store.state.department.department[m].name) {
                    tempAcceptDeptid = this.$store.state.department.department[m].id;
                }
            }
            // await workOfYearTargetApi
            //     .avoidCycleDepartment(this.taskInfos.yearTargetId, this.taskInfos.id, 1, 1, tempAcceptDeptid)
            //     .then((res) => {
            //         for (let j = 0; j < res.data.length; ++j) {
            //             this.departmentOptions[j] = {
            //                 value: res.data[j].id,
            //                 label: res.data[j].name
            //             };
            //         }
            //         // console.log(this.departmentOptions);
            //     });
        },
        getList() {
            this.getTaskTwoLevelByAllocateId();
            this.getWorkByTaskId();
            this.$emit('refresh');
        },
        // 获取部门的下发任务列表
        async getTaskTwoLevelByAllocateId() {
            await workOfYearTargetApi.getTargetTwo(this.taskInfos.id, 1, this.departmentId).then((res) => {
                this.taskData = res.data;
            });
        },
        // 获取当前部门下布置的工作
        async getWorkByTaskId() {
            await workApi.getWorkById(this.taskInfos.id).then((res) => {
                this.workData = res.data;

                for (let i = 0; i < res.data.length; ++i) {
                    this.workData[i].startTime = moment(this.workData[i].startTime).format('YYYY-MM-DD');
                    this.workData[i].endTime = moment(this.workData[i].endTime).format('YYYY-MM-DD');
                }
            });
        },

        // 新增布置的工作
        async addWorkByTargetId() {
                        if (isNaN(this.addWorkInfoForm.number)) {
                this.$alert('序号非法！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
                        if (
                this.addWorkInfoForm.managerId == '' ||
                this.addWorkInfoForm.managerId == null ||
                this.addWorkInfoForm.managerId == undefined
            ) {
                this.$alert('负责人不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (
                this.addWorkInfoForm.helperIds == '' ||
                this.addWorkInfoForm.helperIds == null ||
                this.addWorkInfoForm.helperIds == undefined
            ) {
                this.$alert('协作人不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.addWorkInfoForm.number == '' || this.addWorkInfoForm.number == null || this.addWorkInfoForm.number == undefined) {
                this.$alert('序号不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.addWorkInfoForm.name == '' || this.addWorkInfoForm.name == null || this.addWorkInfoForm.name == undefined) {
                this.$alert('任务标题不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (
                this.addWorkInfoForm.startTime == '' ||
                this.addWorkInfoForm.startTime == null ||
                this.addWorkInfoForm.startTime == undefined
            ) {
                this.$alert('开始日期不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.addWorkInfoForm.endTime == '' || this.addWorkInfoForm.endTime == null || this.addWorkInfoForm.endTime == undefined) {
                this.$alert('结束日期不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            this.addWorkInfoForm.helperIds = this.addWorkInfoForm.helperIds.toString();
            this.addWorkInfoForm.startTime = moment(this.addWorkInfoForm.startTime).format('YYYY-MM-DD');
            this.addWorkInfoForm.endTime = moment(this.addWorkInfoForm.endTime).format('YYYY-MM-DD');
            this.addWorkInfoForm.targetId = this.taskInfos.id;
            await workApi.addWorkByTargetId(this.taskInfos.id, this.addWorkInfoForm).then((res) => {
                this.$message.success('布置任务成功！');
                this.getWorkByTaskId();
                this.addWorkDialogVisible = false;
            });
        },
        cancelAddWorkByTargetId() {
            this.addWorkInfoForm = {};
            this.addWorkDialogVisible = false;
        },
        async editWorkById(id) {
            await workApi.getWorkByIdEdit(id).then((res) => {
                this.editWorkInfoForm = res.data;
                let reslut = [];
                let arrString = this.editWorkInfoForm.helperIds.split(',');
                for (let arr in arrString) {
                    reslut.push(arrString[arr]);
                }
                this.editWorkInfoForm.helperIds = reslut;
                this.editWorkDialogVisible = true;
            });
        },
        cancelEditWork() {
            this.editWorkDialogVisible = false;
            this.editWorkInfoForm = {};
        },
        async pushEditWorkById() {
                       if (isNaN(this.editWorkInfoForm.number)) {
                this.$alert('序号非法！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            } 
            if (
                this.editWorkInfoForm.managerId == '' ||
                this.editWorkInfoForm.managerId == null ||
                this.editWorkInfoForm.managerId == undefined
            ) {
                this.$alert('负责人不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (
                this.editWorkInfoForm.helperIds == '' ||
                this.editWorkInfoForm.helperIds == null ||
                this.editWorkInfoForm.helperIds == undefined
            ) {
                this.$alert('协作人不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.editWorkInfoForm.number == '' || this.editWorkInfoForm.number == null || this.editWorkInfoForm.number == undefined) {
                this.$alert('序号不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.editWorkInfoForm.name == '' || this.editWorkInfoForm.name == null || this.editWorkInfoForm.name == undefined) {
                this.$alert('任务标题不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (
                this.editWorkInfoForm.startTime == '' ||
                this.editWorkInfoForm.startTime == null ||
                this.editWorkInfoForm.startTime == undefined
            ) {
                this.$alert('开始日期不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            if (this.editWorkInfoForm.endTime == '' || this.editWorkInfoForm.endTime == null || this.editWorkInfoForm.endTime == undefined) {
                this.$alert('结束日期不能为空！', '警告', {
                    confirmButtonText: '确定'
                });
                return;
            }
            this.editWorkInfoForm.helperIds = this.editWorkInfoForm.helperIds.toString();

            this.editWorkInfoForm.startTime = moment(this.editWorkInfoForm.startTime).format('YYYY-MM-DD');
            this.editWorkInfoForm.endTime = moment(this.editWorkInfoForm.endTime).format('YYYY-MM-DD');
            await workApi.editWorkById(this.editWorkInfoForm.id, this.editWorkInfoForm).then((res) => {
                this.$message.success('更改工作信息成功！');
                this.editWorkDialogVisible = false;
                this.getWorkByTaskId();
            });
        },
        async deleteWorkById(id) {
            const confirmResult = await this.$confirm('此操作将永久删除该项目, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch((err) => err);
            if (confirmResult != 'confirm') {
                return;
            }
            if (confirmResult == 'confirm') {
                await workApi.deleteWorkById(id).then((res) => {
                    this.$message({
                        message: '删除目标任务成功！',
                        type: 'success'
                    });
                });
                this.getWorkByTaskId();
                this.getList();
            }
        },
        async changeAllocUserIdtoName() {
            await userApi.getUserById(this.taskInfos.allocUserId).then((res) => {
                console.log(res);
                this.taskInfos.allocUserId = res.data.user.name;
            });
        }
    },
    created() {},
    watch: {
        taskInfos() {
            this.getTeachersAndDepartments();
            this.getList();
            this.isPermission()
            for (let i = 0; i < this.$store.state.department.department.length; ++i) {
                if (this.taskInfos.acceptDeptId == this.$store.state.department.department[i].name) {
                    if (this.$store.state.department.department[i].type == 2) {
                        this.isDept = false;
                    } else {
                        this.isDept = true;
                    }
                }
            }
            this.changeAllocUserIdtoName();
        }
    }
};
</script>

<style scoped>
.main {
    color: #606266;
}
.el-row {
    margin-bottom: 10px;
}
</style>